<?
require_once "BaseAction.php";
class UserController extends Tamtay_Base_Action
{
	public function indexAction()
	{
		exit();
	}
	
	public function loginAction()
	{
		//$this->_helper->getHelper('Redirector')->gotoUrlAndExit(IDS_LOGIN_URL);
		
		$u=$this->getUser();
		if ($u->uid>0)
		{
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit("/");
		}
		if ($this->getRequest()->isPost())
		{
			$username=$this->_getParam("username");
			$password=$this->_getParam("password");
			$password=md5($password);
			$remember=$this->_getParam("saveme", 0);
			
			$db=Zend_Registry::get("session_db");
			$sql="SELECT * FROM users WHERE name=? AND pass=?";
			$stm=$db->query($sql, array($username, $password));
			$u=$stm->fetch();
			if ($u)
			{
				$u=$this->unpack($u);
				
				$sess=Zend_Session::getSaveHandler();
				$sess->updateLastLogin($u->uid);
				
				$dbMaster=Zend_Registry::get("master_db");
				$sql="SELECT * FROM users WHERE uid=?";
				$rs=$dbMaster->query($sql, $u->uid);
				$vUser = $rs->fetch();
				if ($vUser){
					if($vUser->avatar != $u->picture){
						$sql="UPDATE users SET avatar=? WHERE uid=?";
						$dbMaster->query($sql, array($u->picture, $u->uid));
					}
				}
				else{
					$sql = "INSERT INTO users(uid, name, avatar, mail, created) VALUES(?, ?, ?, ?, ?)";
					$dbMaster->query($sql, array($u->uid, $u->name, $u->picture, $u->mail, time()));
				}
				
				if ($remember==1)
				{
					$cookieParams = session_get_cookie_params();
					session_set_cookie_params(
							PHOTO_SESSION_TTL,
							$cookieParams['path'],
							$cookieParams['domain'],
							$cookieParams['secure']
							);
				}
				$sess->regenerateSessionID();
				
				Zend_Registry::set("user", $u);
        $ref = $this->_getParam('ref');
        if (!isset($ref) && isset($_SERVER['HTTP_REFERER']))
				  $ref=$_SERVER['HTTP_REFERER'];
				if (isset($ref) && $ref!="")
				{
					$this->_helper->getHelper('Redirector')->gotoUrlAndExit($ref);
				}
				else
				{
					$this->_helper->getHelper('Redirector')->gotoUrlAndExit("/");
				}
			}
			else
			{
				$this->view->is_login_fail=true;
			}
		}
		else
		{
			$username="";
			$this->view->is_login_fail=false;
      $ref = $this->_getParam('ref');
      if (!isset($ref) && isset($_SERVER['HTTP_REFERER']))
        $ref=$_SERVER['HTTP_REFERER'];
      if (isset($ref) && $ref!="")
        $this->view->ref = $ref;
      else
        $this->view->ref = "";
		}
		$this->view->username=$username;
	}
	
	public function logoutAction()
	{
		$this->_helper->getHelper('Redirector')->gotoUrlAndExit(IDS_LOGOUT_URL);
		
		$u=$this->getUser();
		if ($u->uid==0)
		{
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit("/");
		}
		Zend_Session::destroy();
		$sess=Zend_Session::getSaveHandler();
		
		$u=$sess->getAnonymousUser();
		Zend_Registry::set('user', $u);
		$ref=$_SERVER['HTTP_REFERER'];
		if ($ref!="")
		{
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit($ref);
		}
		else
		{
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit("/");
		}
	}
	
	private function unpack($obj) {
		if ($obj->data && $data = unserialize($obj->data)) 
		{
			foreach ($data as $key => $value) {
				if (!isset($obj->$key)) {
					$obj->$key = $value;
				}
			}
		}
		return $obj;
	}
}
?>

